package com.atguigu.algorithm;
class TreeNode{
    int val;
    TreeNode left;
    TreeNode right;
    TreeNode() {}
    TreeNode(int val) { this.val = val; }
    TreeNode(int val, TreeNode left, TreeNode right) {
        this.val = val;
        this.left = left;
        this.right = right;
    }
}
public class UsePreInToForm {
    int index=0;
    public TreeNode buildTree(int[] preorder,int[] inorder){
        return buildTree(preorder,inorder,0,inorder.length);
    }
    public TreeNode buildTree(int[] preorder,int[] inorder,int left,int right){
        if(left>=right){
            return null;
        }
        TreeNode root=new TreeNode(preorder[index]);
        int rootIdx=left;
        while(rootIdx<right){
            if(inorder[rootIdx]==preorder[index]){
                break;
            }
            rootIdx++;
        }
        index++;
        root.left=buildTree(preorder,inorder,left,rootIdx);
        root.right=buildTree(preorder,inorder,rootIdx+1,right);
        return root;
    }
}
